/*   1:    */ package org.jetbrains.io;
/*   2:    */ 
/*   3:    */

import com.intellij.openapi.util.text.StringUtil;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.DefaultFileRegion;
import io.netty.handler.codec.http.*;
import io.netty.handler.codec.http.HttpHeaders.Names;
import io.netty.handler.ssl.SslHandler;
import io.netty.handler.stream.ChunkedFile;

import javax.activation.MimetypesFileTypeMap;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;

/*   4:    */
/*   5:    */
/*   6:    */
/*   7:    */
/*   8:    */
/*   9:    */
/*  10:    */
/*  11:    */
/*  12:    */
/*  13:    */
/*  14:    */
/*  15:    */
/*  16:    */
/*  17:    */
/*  18:    */
/*  19:    */
/*  20:    */
/*  21:    */
/*  22:    */
/*  23:    */
/*  24:    */
/*  25:    */
/*  26:    */
/*  27:    */
/*  28:    */ 
/*  29:    */ public class FileResponses
/*  30:    */ {
/*  31: 39 */   private static final MimetypesFileTypeMap a = new MimetypesFileTypeMap();
/*  32:    */   
/*  33:    */   public static String getContentType(String paramString)
/*  34:    */   {
/*  35: 42 */     return a.getContentType(paramString);
/*  36:    */   }
/*  37:    */   
/*  38:    */   private static boolean a(HttpRequest paramHttpRequest, Channel paramChannel, long paramLong)
/*  39:    */   {
/*  40: 46 */     String str = paramHttpRequest.headers().get(HttpHeaders.Names.IF_MODIFIED_SINCE);
/*  41: 47 */     if (!StringUtil.isEmpty(str)) {
/*  42:    */       try
/*  43:    */       {
/*  44: 49 */         if (((DateFormat)Responses.DATE_FORMAT.get()).parse(str).getTime() >= paramLong)
/*  45:    */         {
/*  46: 50 */           Responses.send(Responses.response(HttpResponseStatus.NOT_MODIFIED), paramChannel, paramHttpRequest);
/*  47: 51 */           return true;
/*  48:    */         }
/*  49:    */       }
/*  50:    */       catch (ParseException localParseException) {}catch (NumberFormatException localNumberFormatException) {}
/*  51:    */     }
/*  52: 59 */     return false;
/*  53:    */   }
/*  54:    */   
/*  55:    */   public static void sendFile(HttpRequest paramHttpRequest, Channel paramChannel, File paramFile)
/*  56:    */     throws IOException
/*  57:    */   {
/*  58: 63 */     if (a(paramHttpRequest, paramChannel, paramFile.lastModified())) {
/*  59: 64 */       return;
/*  60:    */     }
/*  61: 67 */     DefaultHttpResponse localDefaultHttpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
/*  62: 68 */     localDefaultHttpResponse.headers().add(HttpHeaders.Names.CONTENT_TYPE, getContentType(paramFile.getPath()));
/*  63: 69 */     Responses.addCommonHeaders(localDefaultHttpResponse);
/*  64: 70 */     localDefaultHttpResponse.headers().set(HttpHeaders.Names.CACHE_CONTROL, "private, must-revalidate");
/*  65: 71 */     localDefaultHttpResponse.headers().set(HttpHeaders.Names.LAST_MODIFIED, ((DateFormat)Responses.DATE_FORMAT.get()).format(new Date(paramFile.lastModified())));
/*  66:    */     
/*  67: 73 */     boolean bool = Responses.addKeepAliveIfNeed(localDefaultHttpResponse, paramHttpRequest);
/*  68:    */     
/*  69: 75 */     int i = 0;
/*  70:    */     RandomAccessFile localRandomAccessFile;
/*  71:    */     try
/*  72:    */     {
/*  73: 78 */       localRandomAccessFile = new RandomAccessFile(paramFile, "r");
/*  74:    */     }
/*  75:    */     catch (FileNotFoundException localFileNotFoundException)
/*  76:    */     {
/*  77: 81 */       Responses.send(Responses.response(HttpResponseStatus.NOT_FOUND), paramChannel, paramHttpRequest);
/*  78: 82 */       return;
/*  79:    */     }
/*  80:    */     try
/*  81:    */     {
/*  82: 86 */       long l = localRandomAccessFile.length();
/*  83: 87 */       if (paramHttpRequest.getMethod() != HttpMethod.HEAD) {
/*  84: 88 */         HttpHeaders.setContentLength(localDefaultHttpResponse, l);
/*  85:    */       }
/*  86: 91 */       paramChannel.write(localDefaultHttpResponse);
/*  87: 92 */       if (paramHttpRequest.getMethod() != HttpMethod.HEAD) {
/*  88: 93 */         if (paramChannel.pipeline().get(SslHandler.class) == null) {
/*  89: 95 */           paramChannel.write(new DefaultFileRegion(localRandomAccessFile.getChannel(), 0L, l));
/*  90:    */         } else {
/*  91: 99 */           paramChannel.write(new ChunkedFile(localRandomAccessFile));
/*  92:    */         }
/*  93:    */       }
/*  94:102 */       i = 1;
/*  95:    */     }
/*  96:    */     finally
/*  97:    */     {
/*  98:105 */       if (i == 0) {
/*  99:106 */         localRandomAccessFile.close();
/* 100:    */       }
/* 101:    */     }
/* 102:110 */     ChannelFuture localChannelFuture = paramChannel.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
/* 103:111 */     if (!bool) {
/* 104:112 */       localChannelFuture.addListener(ChannelFutureListener.CLOSE);
/* 105:    */     }
/* 106:    */   }
/* 107:    */ }



/* Location:           E:\idea\

 * Qualified Name:     org.jetbrains.io.FileResponses

 * JD-Core Version:    0.7.0.1

 */